<?php

namespace app\api\controller;

use app\admin\model\inventory\Kc;
use app\common\controller\Api;
use app\common\library\Log;
use app\common\model\Kcmx;
use think\Config;
use think\Db;
use think\Exception;

/**
 * 仓库基础资料
 */
class Update extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取仓库列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function test()
    {
        $arr = array(array(array()),array(),array());
        if(empty($arr)){
            echo 111;
        }
        if (!array_filter(array_map('array_filter', $arr))) {
            echo 'The array is completely empty';
        }


        $arr2 = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6);
        $resArr2=array_filter($arr2,'fun_odd');//其中的fun_odd必须加引号，不能加()
        print_r($resArr2);

    }

    public function insert(){

        $data  = [
            'header' => [
                'code' => '订单编号',
                'order_date' => '日期',
                'wh_code' => '仓库编码',
                'shelves_code'=>'货架编码',
                'createtime' => '创建时间2023-6-3 23:59:59',
                'sh_time' => '审核时间2023-6-3 23:59:59',
                'applyer'=>'申请人',
            ],
            'item'=>[[
                         'quantity'=>'数量',
                         'quantity2'=>'数量',
                         'inv_code'=>'物料编码',
                         'free'=>'规格属性',
                         'unit_code'=>'单位',
                         'unit_code2'=>'换算单位'
                     ],
            ],
        ];



        $header = $data['header'];
        $item   = $data['item'];
        $warehouseCode = $header['wh_code'];
        $shelvesCode  = $header['shelves_code'];

        $warehouseId = Db::name('warehouse')->where('number',$warehouseCode)->value('id');
        $shelvesId = Db::name('shelves')->where('number',$shelvesCode)->value('id');

        $temporary =[
            'order_code'=>$header['code'],
            'order_date'=>$header['order_date'],
            'order_list'=>3,
            'warehouse_id'=>$warehouseId,
            'shelves_id'=>$shelvesId,
            'grounding_list'=>1,
            'status_list'=>1,
            'createtime'=>$header['createtime'],

        ];

        Db::startTrans();
        try {
            Db::name('temporary')->inset($temporary);
            //入库
            foreach ($item as $k=>$v){
                //更新本地库存
                Kc::rdRecordUpdateKc($v['inv_code'],$v['quantity'],$header['code'],$v['order_date'],1,5);
                //插入中间库
                $list[] = [
                    'cInvCode'=>$v['inv_code'],
                    'iQuantity'=>$v['code'],
                    'createtime'=>date('Y-m-d H:i:s'),
                ];
            }
            $this->insertMiddlData($warehouseCode,$header['order_date'],$header['code'],$list);

        } catch (Exception $e) {

            Db::rollback();
            $this->error('处理失败,' . $e->getMessage());
        }

    }

    public function insertMiddlData($cWhCode,$dDate,$cCode,$list)
    {


        //插入中间库
        $insert_id = Db::connect('database.inc')->table('RdRecord08')->insertGetId([
            'bRdFlag'=>1,
            'cVouchType' =>'08' ,
            'cSource' => '库存',
            'cWhCode' => $cWhCode,
            'dDate'=>$dDate,
            'cCode'=>$cCode,
            'bIsSTQc'=>1,
            'bTransFlag'=>1,
            'ID'=>time(),
        ]);
        foreach ($list as $k=>$v){
            Db::connect('database.inc')->table('rdrecords08')->insert([
                'cInvCode'=>$v['cInvCode'],
                'ID' => $insert_id,
                'autoID'=>time(),
                'iQuantity' => $v['iQuantity'],
                'iFlag' => 0,
            ]);
        }
    }



}
